import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { DrugInfoApi } from "@/api/erp/drug/drugInfo";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
let reload: Function;
let id: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage.drugManagement.drugInfo";
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.code`),
			field: "code",
			component: "Input"
		},
		{
			label: t(`${en_vi}.name`),
			field: "name",
			component: "Input"
		},
		{
			label: t(`${en_vi}.dosageForm`),
			field: "dosageForm",
			component: "Input"
		},
		{
			label: t(`${en_vi}.content`),
			field: "content",
			component: "Input"
		},
		{
			label: t(`${en_vi}.inspectionExpiry`),
			field: "inspectionExpiry",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.inspectionDate`),
			field: "inspectionDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.contractDate`),
			field: "contractDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.startDate`),
			field: "startDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.endDate`),
			field: "endDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.sendDate`),
			field: "sendDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.approvalDate`),
			field: "approvalDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.groupCode`),
			field: "groupCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.typeCode`),
			field: "typeCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.productCode`),
			field: "productCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.countryCode`),
			field: "countryCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.departmentGroup`),
			field: "departmentGroup",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.insuranceType`),
			field: "insuranceType",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.essential`),
			field: "essential",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.accountNumber`),
			field: "accountNumber",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.ingredientName`),
			field: "ingredientName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.ingredientCode`),
			field: "ingredientCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.quantity`),
			field: "quantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.groupField`),
			field: "groupField",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.registrationNumber`),
			field: "registrationNumber",
			component: "Input"
		},
		{
			label: t(`${en_vi}.unitPrice`),
			field: "unitPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.minQuantity`),
			field: "minQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.salePrice`),
			field: "salePrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.repaymentDays`),
			field: "repaymentDays",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.dutyDrug`),
			field: "dutyDrug",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.postIssuance`),
			field: "postIssuance",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.ratio`),
			field: "ratio",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.serialNumber`),
			field: "serialNumber",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.imagePath`),
			field: "imagePath",
			component: "Input"
		},
		{
			label: t(`${en_vi}.typeField`),
			field: "typeField",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.power`),
			field: "power",
			component: "Input"
		},
		{
			label: t(`${en_vi}.printGroup`),
			field: "printGroup",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.occupationalRelated`),
			field: "occupationalRelated",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.unitCode`),
			field: "unitCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.unit`),
			field: "unit",
			component: "Input"
		},
		{
			label: t(`${en_vi}.patientType`),
			field: "patientType",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.rounding`),
			field: "rounding",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.days`),
			field: "days",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.hidden`),
			field: "hidden",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.wholeIssue`),
			field: "wholeIssue",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.packagingQuantity`),
			field: "packagingQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.targetCode`),
			field: "targetCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.salePrice2`),
			field: "salePrice2",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.imageData`),
			field: "imageData",
			component: "Upload" // 假设使用上传组件
		},
		{
			label: t(`${en_vi}.productionDosageForm`),
			field: "productionDosageForm",
			component: "Input"
		},
		{
			label: t(`${en_vi}.usageDosageForm`),
			field: "usageDosageForm",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcCode`),
			field: "atcCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.genericName`),
			field: "genericName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.atcClassificationCode`),
			field: "atcClassificationCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.insurancePrice`),
			field: "insurancePrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.technologyType`),
			field: "technologyType",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.priceDifferenceFlag`),
			field: "priceDifferenceFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.abcClassification`),
			field: "abcClassification",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.drugType`),
			field: "drugType",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.classificationType`),
			field: "classificationType",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.components`),
			field: "components",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.indications`),
			field: "indications",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.contraindications`),
			field: "contraindications",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.precautions`),
			field: "precautions",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.pregnancyUsage`),
			field: "pregnancyUsage",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.interactions`),
			field: "interactions",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.adverseReactions`),
			field: "adverseReactions",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.dosage`),
			field: "dosage",
			component: "Input"
		},
		{
			label: t(`${en_vi}.administrationRoute`),
			field: "administrationRoute",
			component: "Input"
		},
		{
			label: t(`${en_vi}.methodOfUse`),
			field: "methodOfUse",
			component: "Input"
		},
		{
			label: t(`${en_vi}.warehouseCode`),
			field: "warehouseCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.bidPrice`),
			field: "bidPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.abbreviation`),
			field: "abbreviation",
			component: "Input"
		},
		{
			label: t(`${en_vi}.dialysisFlag`),
			field: "dialysisFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.eachDose`),
			field: "eachDose",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.course`),
			field: "course",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.hospitalPreparation`),
			field: "hospitalPreparation",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.phone`),
			field: "phone",
			component: "Input"
		},
		{
			label: t(`${en_vi}.hospitalCode`),
			field: "hospitalCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.useUnit`),
			field: "useUnit",
			component: "Input"
		},
		{
			label: t(`${en_vi}.prescriptionOtcFlag`),
			field: "prescriptionOtcFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.pendingApproval`),
			field: "pendingApproval",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.policyNumber`),
			field: "policyNumber",
			component: "Input"
		},
		{
			label: t(`${en_vi}.shortName`),
			field: "shortName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.filterCondition`),
			field: "filterCondition",
			component: "Input"
		},
		{
			label: t(`${en_vi}.volume`),
			field: "volume",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.rhdClassification`),
			field: "rhdClassification",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.surcharge`),
			field: "surcharge",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.manufacturerCode`),
			field: "manufacturerCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.bidCode`),
			field: "bidCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.lossRate`),
			field: "lossRate",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.reserveLock`),
			field: "reserveLock",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.paymentObject`),
			field: "paymentObject",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.gmpCertification`),
			field: "gmpCertification",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.stockQuantity`),
			field: "stockQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.catalogFlag`),
			field: "catalogFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.userId`),
			field: "userId",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.vat`),
			field: "vat",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.orderNumber`),
			field: "orderNumber",
			component: "Input"
		},
		{
			label: t(`${en_vi}.appendix3Number`),
			field: "appendix3Number",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.medicalLevel`),
			field: "medicalLevel",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.departmentCode`),
			field: "departmentCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.remarks`),
			field: "remarks",
			component: "InputTextArea"
		},
		{
			label: t(`${en_vi}.bidQuantity`),
			field: "bidQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.procurementMethod`),
			field: "procurementMethod",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.bidStorageQuantity`),
			field: "bidStorageQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.outsideHours`),
			field: "outsideHours",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.bidYear`),
			field: "bidYear",
			component: "Input"
		},
		{
			label: t(`${en_vi}.contractNumber`),
			field: "contractNumber",
			component: "Input"
		},

		{
			label: t(`${en_vi}.compensationDecision`),
			field: "compensationDecision",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.objectGroup`),
			field: "objectGroup",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.icdCode`),
			field: "icdCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.minimumPrice`),
			field: "minimumPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.hospitalId`),
			field: "hospitalId",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.regulation22Code`),
			field: "regulation22Code",
			component: "Input"
		},
		{
			label: t(`${en_vi}.warningGroup`),
			field: "warningGroup",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.regulation2182Code`),
			field: "regulation2182Code",
			component: "Input"
		},
		{
			label: t(`${en_vi}.regulation2182Usage`),
			field: "regulation2182Usage",
			component: "Input"
		},
		{
			label: t(`${en_vi}.hasRecord`),
			field: "hasRecord",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.componentCode5084`),
			field: "componentCode5084",
			component: "Input"
		},
		{
			label: t(`${en_vi}.doctorCode`),
			field: "doctorCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.bidUsagePeriod`),
			field: "bidUsagePeriod",
			component: "Input"
		},
		{
			label: t(`${en_vi}.secondBidQuantity`),
			field: "secondBidQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.warehouseGroup`),
			field: "warehouseGroup",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.averageQuantity`),
			field: "averageQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.hospitalGroup`),
			field: "hospitalGroup",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.largeExpenseFlag`),
			field: "largeExpenseFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.usageMinutes`),
			field: "usageMinutes",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.storageReserveLock`),
			field: "storageReserveLock",
			component: "Input"
		},
		{
			label: t(`${en_vi}.insurancePrice2`),
			field: "insurancePrice2",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.paymentTargetCode`),
			field: "paymentTargetCode",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.typeCode2`),
			field: "typeCode2",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.warningFlag`),
			field: "warningFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.insuranceDrug`),
			field: "insuranceDrug",
			component: "Input"
		},
		{
			label: t(`${en_vi}.serviceDrug`),
			field: "serviceDrug",
			component: "Input"
		},
		{
			label: t(`${en_vi}.defectiveQuantity`),
			field: "defectiveQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.insuranceType2`),
			field: "insuranceType2",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.taxExcludedPrice`),
			field: "taxExcludedPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.bidStatus`),
			field: "bidStatus",
			component: "Input"
		},

		{
			label: t(`${en_vi}.lockReserveTarget`),
			field: "lockReserveTarget",
			component: "Input"
		},
		{
			label: t(`${en_vi}.lockReserveCategory`),
			field: "lockReserveCategory",
			component: "Input"
		},
		{
			label: t(`${en_vi}.usageYear`),
			field: "usageYear",
			component: "Input"
		},
		{
			label: t(`${en_vi}.productionYear`),
			field: "productionYear",
			component: "Input"
		},
		{
			label: t(`${en_vi}.source`),
			field: "source",
			component: "Input"
		},

		{
			label: t(`${en_vi}.department`),
			field: "department",
			component: "Input"
		},
		{
			label: t(`${en_vi}.inspectionUnit`),
			field: "inspectionUnit",
			component: "Input"
		},
		{
			label: t(`${en_vi}.salesRatioGroup`),
			field: "salesRatioGroup",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.insuranceSales`),
			field: "insuranceSales",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.serviceSales`),
			field: "serviceSales",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.targetCode1`),
			field: "targetCode1",
			component: "Input"
		},
		{
			label: t(`${en_vi}.axName`),
			field: "axName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.insurance30Days`),
			field: "insurance30Days",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.ingredientCode30`),
			field: "ingredientCode30",
			component: "Input"
		},
		{
			label: t(`${en_vi}.pharmacyCode`),
			field: "pharmacyCode",
			component: "Input"
		},
		{
			label: t(`${en_vi}.storageConditions`),
			field: "storageConditions",
			component: "Input"
		},
		{
			label: t(`${en_vi}.registrationStatus`),
			field: "registrationStatus",
			component: "Input"
		},
		{
			label: t(`${en_vi}.vatGtgt`),
			field: "vatGtgt",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.lockInsurance`),
			field: "lockInsurance",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.retailPrice`),
			field: "retailPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.packagingUnit`),
			field: "packagingUnit",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.additionalFee`),
			field: "additionalFee",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.approvalNumber`),
			field: "approvalNumber",
			component: "Input"
		},
		{
			label: t(`${en_vi}.approvedQuantity`),
			field: "approvedQuantity",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.approvedUnitPrice`),
			field: "approvedUnitPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.storageLocation`),
			field: "storageLocation",
			component: "Input"
		},
		{
			label: t(`${en_vi}.contraindicatedIcd`),
			field: "contraindicatedIcd",
			component: "Input"
		},
		{
			label: t(`${en_vi}.hl7Id`),
			field: "hl7Id",
			component: "Input"
		},
		{
			label: t(`${en_vi}.preparationSerial`),
			field: "preparationSerial",
			component: "Input"
		},
		{
			label: t(`${en_vi}.fileName`),
			field: "fileName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.standardPriceFlag`),
			field: "standardPriceFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.code5086`),
			field: "code5086",
			component: "Input"
		},
		{
			label: t(`${en_vi}.paymentStatus`),
			field: "paymentStatus",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.regulation130`),
			field: "regulation130",
			component: "Input"
		},
		{
			label: t(`${en_vi}.vaccineFlag`),
			field: "vaccineFlag",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.insuranceQuantity`),
			field: "insuranceQuantity",
			component: "InputNumber"
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t("felix_ui.edit") : t("felix_ui.add");
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 12
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		id = data.record?.id;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		let formValue = await validateFields();
		formValue = {
			...formValue,
			contractDate: Date.parse(formValue.contractDate),
			startDate: Date.parse(formValue.startDate),
			endDate: Date.parse(formValue.endDate),
			sendDate: Date.parse(formValue.sendDate),
			approvalDate: Date.parse(formValue.approvalDate),
			inspectionExpiry: Date.parse(formValue.inspectionExpiry),
			inspectionDate: Date.parse(formValue.inspectionDate)
		};
		let res;
		if (isUpdated) {
			formValue.id = id;
			res = await DrugInfoApi.updateDrugInfo(formValue);
		} else {
			res = await DrugInfoApi.createDrugInfo(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	return (
		<BasicDrawer size="large" title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
